const express = require("express");
const fs = require("fs");
const app = express();
//中间件
app.use(express.static("./www"));

// 路由中间件 处理GET请求
app.get("/res/1", (req, res) => {
  // res.type("html");
  res.send("<h1>HTML String</h1>");
});
app.get("/res/2", (req, res) => {
  // JSON
  // res.type("json");
  res.send({ title: "JSON", name: "jack", age: 20 });
});

app.get("/res/3", (req, res) => {
  // xml
  res.type("xml");
  res.send(`
    <user>
      <title>XML</title>
      <name>jack</name>
      <age>20</age>
    </user>
  `);
});

app.get("/res/4", (req, res) => {
  res.sendFile("F:/code-212/20240124/www/1.html");
});

app.get("/res/5", (req, res) => {
  res.cookie("name", "jack");
  res.cookie("age", 20);
  res.send("hello cookie");
});

app.get("/res/6", (req, res) => {
  res.cookie("name", "jack");
  res.status(200);
  res.type("json");
  res.send({ name: "jack" });
});

// 设置端口
const port = 3008;
app.listen(port, () => {
  console.log(`http://192.168.1.42:${port}`);
});
